package com.app.task;/**
 * @program: app-admin
 * @name CheckDeviceTask
 * @description: 业务处理定时任务
 */

import com.app.common.constant.CacheConstants;
import com.app.common.core.redis.RedisCache;
import com.app.common.utils.StringUtils;
import com.app.common.utils.spring.SpringUtils;
import com.app.stick.bind.domain.BizPatientDeviceInfo;
import com.app.stick.bind.mapper.BizPatientDeviceInfoMapper;
import com.app.stick.monitor.domain.vo.BizMonitorCacheVo;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Date;

/**
 *@author:
 *@program: app-admin
 *@name CheckDeviceTask
 *@description: 业务处理定时任务
 */
@Component("businessTask")
public class BusinessTask {

    /**
     * 监测设备状态
     */
    public void monitorDeviceStatus(Integer intervalDuration){
        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        BizPatientDeviceInfoMapper bizPatientDeviceInfoMapper = SpringUtils.getBean(BizPatientDeviceInfoMapper.class);
        Collection<String> keys = redisCache.keys(CacheConstants.DEVICE_MONITOR_KEY + "*");
        for (String key : keys) {
            BizMonitorCacheVo cacheVo = redisCache.getCacheObject(key);
            if (StringUtils.isNull(cacheVo)) {
                continue;
            }
            long intervalSeconds = cacheVo.getIntervalSeconds();
            if (intervalSeconds >= intervalDuration) {
                //去除
                redisCache.deleteObject(CacheConstants.DEVICE_MONITOR_KEY + key);
                //然后记录时间
                BizPatientDeviceInfo bizPatientDeviceInfo = bizPatientDeviceInfoMapper.selectById(cacheVo.getPdiId());
                if (StringUtils.isNotNull(bizPatientDeviceInfo)) {
                    BizPatientDeviceInfo patientDeviceInfo = new BizPatientDeviceInfo();
                    patientDeviceInfo.setPdiId(bizPatientDeviceInfo.getPdiId());
                    patientDeviceInfo.setPdiCurDuration(intervalSeconds);
                    patientDeviceInfo.setPdiAccDuration(bizPatientDeviceInfo.getPdiAccDuration() + intervalSeconds);
                    patientDeviceInfo.setUpdateTime(new Date());
                    bizPatientDeviceInfoMapper.updateById(patientDeviceInfo);
                }
            }
        }
    }
}
